<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建会员':'编辑会员'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="memberForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.id" hidden/>
        </el-form-item>
        <el-form-item hidden>
          <el-input v-model="form.userId" hidden/>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" readonly>
            <el-button v-if="hasLoaded && !form.id" slot="append" icon="el-icon-search" @click="selectUser" />
          </el-input>
        </el-form-item>
        <el-form-item label="会员等级" prop="levelId">
          <el-select v-model="form.levelId" clearable filterable placeholder="选择会员等级">
            <el-option
              v-for="item in optionOfLevel"
              :key="'member_level_' + item.id"
              :label="item.text"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender">
            <el-option
              v-for="item in optionOfGender"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"/>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="所在城市" prop="city">
          <el-input v-model="form.city"/>
        </el-form-item>
        <el-form-item label="职业" prop="job">
          <el-input v-model="form.job"/>
        </el-form-item>
        <el-form-item label="积分" prop="integration">
          <el-input-number v-model="form.integration" :min="0" />
        </el-form-item>
        <el-form-item label="成长值" prop="growth">
          <el-input-number v-model="form.growth" :min="0" />
        </el-form-item>
        <el-form-item label="用户状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <user-select-dialog v-if="openUserDialog" ref="userDialog" :single="true" @confirmDialog="setUser" />

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { memberLevelSelect, memberLoad, memberSave } from '@/api/system/member'
import { userLoad } from '@/api/system/user'
import UserSelectDialog from '@/views/system/user/table/selectDialog'

export default {
  name: 'MemberFormDialog',
  components: {
    UserSelectDialog
  },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        userId: undefined,
        levelId: undefined,
        username: undefined,
        nickname: undefined,
        mobile: undefined,
        email: undefined,
        avatar: undefined,
        gender: '0',
        birthday: undefined,
        city: undefined,
        job: undefined,
        signature: undefined,
        integration: 0,
        growth: 0,
        status: '0'
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空' }],
        levelId: [{ required: true, message: '会员等级不能为空' }],
        gender: [{ required: true, message: '性别不能为空' }],
        mobile: [{ required: true, message: '手机号不能为空' }],
        status: [{ required: true, message: '用户状态不能为空' }]
      },
      optionOfLevel: [],
      optionOfGender: [],
      optionOfStatus: [],
      openUserDialog: false
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      memberLoad({ id: id }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        this.form.levelId = this.form.levelId + ''
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:member:edit'])
      }).catch(() => {})
    },
    getDictData() {
      const that = this
      this.$http.all([memberLevelSelect(), this.$dictGroup('sys_user_sex'), this.$dictGroup('sys_normal_disable')])
        .then(this.$http.spread(function(level, gender, status) {
          that.optionOfLevel = level.param.data
          that.optionOfGender = gender
          that.optionOfStatus = status
        }))
    },
    selectUser() {
      this.openUserDialog = true
      const selected = this.form.userId
      this.$nextTick(() => {
        this.$refs.userDialog.init()
        if (selected) {
          this.$refs.userDialog.initSelectedData(selected)
        }
      })
    },
    setUser(node) {
      this.form.userId = node[0].userId
      userLoad({ id: this.form.userId }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        this.form.username = response.param.userName
      })
    },
    onSubmit() {
      this.$refs.memberForm.validate(valid => {
        if (valid) {
          memberSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.memberForm.resetFields()
      this.visible = false
    }
  }
}
</script>
